﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Police_Question_QuestionList2 : System.Web.UI.Page
{
    private bool Flag = true;
    private int PageCount = 15;
    private string ConstDeparmentID;
    protected void Page_Load(object sender, EventArgs e)
    {
        ConstDeparmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (!IsPostBack)
        {
            Bind(PageCount, 0);
            Flag = true;
            if (!ConstDeparmentID.Substring(4, 1).Equals("0"))
            {
                Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                return;
            }
        }
    }

    public void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        Bind(pageSize, PageIndex);
    }

    private void Bind(int pageSize, int pageIndex)
    {

        QuestionDataContext db = new QuestionDataContext();
        bool bl = ConstDeparmentID.Substring(2, 3).Equals("000");

        if (Flag)
        {
            if (bl)
            {
                var result = (from s in db.SecuritySubject where s.CreateDepartment == ConstDeparmentID && (from o in db.Associate select o.SID).Contains(s.Id) orderby s.CreateTime descending select s);
                var obj = result.Skip(pageSize * pageIndex).Take(pageSize);
    

                this.UserGrid.DataSource = obj;
                ListPageContral1.ItemCount = result.Count();
                this.UserGrid.DataBind();
            }
            else
            {
                var list =( from dd in db.Associate where dd.DepartmentID.Substring(0,6) == ConstDeparmentID.Substring(0,6) select dd);
                var listresult=from l in list where (from dr in db.Examination where dr.DepartmentID.Substring(0,4)==ConstDeparmentID.Substring(0,4) select dr.SID).Contains(l.SID) select l;

                var result = (from s in db.SecuritySubject where (from ss in listresult select ss.SID).Contains(s.Id)|| ((from a in db.Examination select a.SID).Contains(s.Id)&&s.CreateDepartment==ConstDeparmentID) orderby s.CreateTime descending select s);
                var obj = result.Skip(pageSize * pageIndex).Take(pageSize);  

                this.UserGrid.DataSource = obj;
                ListPageContral1.ItemCount = result.Count();
                this.UserGrid.DataBind();
            }
        }
        else
        {
            if (bl)
            {
                var obj = (from s in db.SecuritySubject where s.Caption.Contains(txtTitle.Text) && s.CreateDepartment == ConstDeparmentID && (from o in db.Associate select o.SID).Contains(s.Id) orderby s.CreateTime descending select s);
                var result = obj.Skip(pageSize * pageIndex).Take(pageSize);
                this.UserGrid.DataSource = result;
                ListPageContral1.ItemCount = obj.Count();
                this.UserGrid.DataBind();
            }
            else
            {
                var list = (from dd in db.Associate where dd.DepartmentID.Substring(0,6) == ConstDeparmentID.Substring(0,6) select dd);
                var listresult = from l in list where (from dr in db.Examination where dr.DepartmentID.Substring(0, 4) == ConstDeparmentID.Substring(0, 4) select dr.SID).Contains(l.SID) select l;
                var obj = (from s in db.SecuritySubject where s.Caption.Contains(txtTitle.Text) && (from ss in listresult select ss.SID).Contains(s.Id) || ((from a in db.Examination select a.SID).Contains(s.Id) && s.CreateDepartment == ConstDeparmentID) orderby s.CreateTime descending select s);
                var result = obj.Skip(pageSize * pageIndex).Take(pageSize);
                this.UserGrid.DataSource = result;
                ListPageContral1.ItemCount = obj.Count();
                this.UserGrid.DataBind();
                
            }
        }
    }

    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (((int)ListPageContral1.CurrentPageIndex) * ListPageContral1.PageSize + (e.Row.RowIndex + 1)).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }


    public string GetDepartmentName(object obj)
    {
        try
        {
            if (obj == null)
                return "";
            else
                return Yipusoft.Account.BLL.Department.GetDepartment(obj.ToString()).ShortName;
        }
        catch
        {
            return "";
        };


    }
    protected void lBtnQuery_Click(object sender, EventArgs e)
    {
        if (txtTitle.Text.Trim() == "")
        {
            Flag = true;
            Response.Redirect("QuestionList2.aspx");
        }
        else
        {
            Flag = false;
        }
        Bind(PageCount, 0);
    }
    protected void rBtnCadre_CheckedChanged(object sender, EventArgs e)
    {
        Response.Redirect("~/Police/Question2/QuestionList2.aspx");
    }

    public string GetHref(string ID)
    {
        if (ConstDeparmentID.Substring(2, 3).Equals("000"))
        {
            return "<a href='ZongExamination2.aspx?ID=" + ID.ToString() + "'>编辑</a>&nbsp;<a href='ProblemList.aspx?ID=" + ID.ToString() + "'>查看详细</a>";
        }
        else
        {
            return "<a href='QuestionNew2.aspx?ID=" + ID.ToString() + "'>编辑</a>&nbsp;<a href='ProblemList.aspx?ID=" + ID.ToString() + "'>查看详细</a>";
        }

        
        
    }
}
